package sort.test3_27;

import sort.QuickSort;

public class QuickSort1 {

    public int[] guickSort(int[] nums) {
        if (nums.length == 0) {
            return null;
        }
        sort(nums,0,nums.length-1);
        return nums;
    }


    //todo:快速排序是整体划分　　大致排一下顺序　　然后再不断调整小部分顺序
//    todo:归并排序　是小部分排序　然后再整体排序　
    public void sort(int[] nums, int lo,int hi) {
        if (lo>=hi){
            return;
        }
        int part = partition(nums, lo, hi);
        sort(nums,lo,part);
        sort(nums,part+1,hi);
    }


    public int  partition(int[] nums, int lo,int hi) {
        int mid=nums[lo];
        int j=lo+1;
        int k=hi;
        while (j<=k){
            while (j<=hi&&nums[j]<=mid){
                j++;
            }
            while (k>=lo&&nums[k]>mid){
                k--;
            }
            if (j>=k){
                break;
            }
            swap(nums,j,k);
        }
        swap(nums,lo,k);
        return k;
    }


    public  void  swap(int[] nums,int j,int k){
        int temp=nums[j];
        nums[j]=nums[k];
        nums[k]=temp;
    }
    public static void main(String[] args) {
        QuickSort1 quickSort = new QuickSort1();
        int[] nums={2,3,41,4,5,32,1,34,32,41};
        quickSort.sort(nums,0, nums.length-1);
        for (int a: nums
        ) {
            System.out.println(a);
        }
    }





}
